package com.arsn.myf.swagger.config;

import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.base.Function;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * swagger2 配置类
 * @author arsn
 * @since 2022-10-10
 */
@EnableSwagger2
@Configuration
public class SwaggerConfig {

    @Bean
    public Docket docketLogin() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("登录模块")
                .select()
                //扫描那些controller
                .apis(RequestHandlerSelectors.basePackage("com.arsn.myf.security.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket docketResource() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("资源模块")
                .select()
                //扫描那些controller
                .apis(RequestHandlerSelectors.basePackage("com.arsn.myf.resource.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket docketSystem() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("系统模块")
                .select()
                //扫描那些controller
                .apis(RequestHandlerSelectors.basePackage("com.arsn.myf.system.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket docketFlowable() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("flowalbe模块")
                .select()
                //扫描那些controller
                .apis(RequestHandlerSelectors.basePackage("com.arsn.myf.flow.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket docketGenerateCode() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("代码生成模块")
                .select()
                //扫描那些controller
                .apis(RequestHandlerSelectors.basePackage("com.arsn.myf.generate.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket docketQuartz() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("定时任务管理模块")
                .select()
                //扫描那些controller
                .apis(RequestHandlerSelectors.basePackage("com.arsn.myf.quartz.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket docketBusiness() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("业务模块")
                .select()
                //扫描那些controller
                .apis(RequestHandlerSelectors.basePackage("com.arsn.myf.business.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private static Predicate<RequestHandler> basePackage(String... basePackages) {
        return input -> declaringClass(input).transform(handlerPackage(basePackages)).or(true);
    }

    private static Optional<? extends Class<?>> declaringClass(RequestHandler input) {
        return Optional.fromNullable(input.declaringClass());
    }

    private static Function<Class<?>, Boolean> handlerPackage(String[] basePackage)     {
        return input -> {
            // 循环判断匹配
            for (String strPackage : basePackage) {
                boolean isMatch = input.getPackage().getName().startsWith(strPackage);
                if (isMatch) {
                    return true;
                }
            }
            return false;
        };
    }

    /**
     * 主页配置
     * @return
     */
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .description("我爱swagger")
                .title("我爱学习")
                .contact(new Contact("zhongrj", "", "arsn163@163.com"))
                .version("1.0.0")
                .license("swagger")
                .licenseUrl("")
                .build();
    }
}
